<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 教学模型
 * @version 1.0.0
 * @author Jason.z
 * @copyright AnAn-Studio.Inc
 */
class Teach_model extends CI_Model
{

    const USERS_TBL = "users";
    const TEACHERS_TBL = "teachers";
    const STUDENTS_TBL = "students";
    const APPLIES_TBL = "applies";
    const CLASSES_TBL = "classes";

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 根据ID获取教师
     *
     * @param $id
     * @return null
     */
    public function get_teacher_by_id($id)
    {
        if(is_numeric($id)) {
            $this->db->where('teacher_id',$id);
            $query = $this->db->get(self::TEACHERS_TBL);
            return $query->row();
        }
        return NULL;
    }

    /**
     * 获取所有教师
     *
     * @param array $where 条件数组
     * @return array 教师数组
     */
    public function get_all_teachers($where=array())
    {
        if(!empty($where)) {
            $this->db->where($where);
        }
        $query = $this->db->get(self::TEACHERS_TBL);
        return $query->result();
    }

    /**
     * 插入教师数据
     *
     * @param $data 教师数据
     */
    public function insert_teacher($data)
    {
        if(is_array($data)&&count($data)) {
            $this->db->insert(self::TEACHERS_TBL,$data);
            return $this->db->insert_id();
        }
        return FALSE;
    }

    /**
     * 更新教师信息
     *
     * @param $id
     * @param $data
     */
    public function update_teacher($id,$data)
    {
        if(is_numeric($id)&&is_array($data)&&count($data)>0) {
            $this->db->where('teacher_id',$id);
            $this->db->update(self::TEACHERS_TBL,$data);
            return $this->db->affected_rows()>0?TRUE:FALSE;
        }
        return FALSE;
    }

    /**
     * 获取所有申请
     *
     * @return mixed
     */
    public function get_all_applies()
    {
        $query = $this->db->get(self::APPLIES_TBL);
        return $query->result();
    }

    /**
     * 插入申请
     *
     * @param $data 数据
     * @return bool 结果
     */
    public function insert_apply($data)
    {
        if(is_array($data)&&count($data)>0) {
            $this->db->insert(self::APPLIES_TBL,$data);
            return $this->db->insert_id();
        }
        return FALSE;
    }

    /**
     * 更新申请
     *
     * @param $data 数据
     * @param $id  对应ID
     * @return bool 结果
     */
    public function update_apply($data,$id)
    {
        if(is_numeric($id)&&is_array($data)&&count($data)>0) {
            $this->db->where('apply_id',$id);
            $this->db->update(self::APPLIES_TBL,$data);
            return $this->db->affected_rows()>0?TRUE:FALSE;
        }
        return FALSE;
    }

    /**
     * 插入课程信息
     *
     * @param $data
     * @return bool
     */
    public function insert_class($data)
    {
        if(is_array($data)&&count($data)>0) {
            $this->db->insert(self::CLASSES_TBL,$data);
            return $this->db->insert_id();
        }
        return FALSE;
    }

    /**
     * 更新课程信息
     *
     * @param $data
     * @param $id
     * @return bool
     */
    public function update_class($data,$id)
    {
        if(is_numeric($id)&&is_array($data)&&count($data)>0) {
            $this->db->where('class_id',$id);
            $this->db->update(self::CLASSES_TBL,$data);
            return $this->db->affected_rows()>0?TRUE:FALSE;
        }
        return FALSE;
    }

    /**
     * 获取所有课程
     *
     * @return mixed
     */
    public function get_teacher_classes($teacher_id=0,$student_id=0)
    {
        $this->db->select(self::TEACHERS_TBL.'.teacher_name,'.self::CLASSES_TBL.'.*,GROUP_CONCAT(bf_classes.date) as days');
        $this->db->join(self::TEACHERS_TBL,self::TEACHERS_TBL.'.teacher_id='.self::CLASSES_TBL.'.teacher_id');
        if(!empty($teacher_id)) {
            $this->db->where(self::CLASSES_TBL.'.teacher_id',$teacher_id);
        }
        if(!empty($student_id)) {
            $this->db->where(self::CLASSES_TBL.'.student_id',$student_id);
        }
        $this->db->group_by(self::CLASSES_TBL.'.teacher_id');
        $this->db->group_by(self::CLASSES_TBL.'.week');
//        $this->db->group_by(self::CLASSES_TBL.'.date');
        $this->db->order_by(self::CLASSES_TBL.'.week','DESC');
        $query = $this->db->get(self::CLASSES_TBL);
//        echo $this->db->last_query();
        return $query->result();
    }

    /**
     * 获取某个课程下的学生
     *
     * @param $teacher_id
     * @param $week
     * @return mixed
     */
    public function get_class_students($teacher_id,$date,$student_id=0)
    {
        $this->db->select(self::STUDENTS_TBL.'.student_name,'.self::CLASSES_TBL.'.*');
        $this->db->join(self::TEACHERS_TBL,self::TEACHERS_TBL.'.teacher_id='.self::CLASSES_TBL.'.teacher_id');
        $this->db->join(self::STUDENTS_TBL,self::STUDENTS_TBL.'.student_id='.self::CLASSES_TBL.'.student_id');
        $this->db->where(self::CLASSES_TBL.'.teacher_id',$teacher_id);
        $this->db->where(self::CLASSES_TBL.'.week',date('W',strtotime($date)));
        if(!empty($student_id)) {
            $this->db->where(self::CLASSES_TBL.'.student_id',$student_id);
        }
        $this->db->order_by(self::CLASSES_TBL.'.time');
        $query = $this->db->get(self::CLASSES_TBL);
//        echo $this->db->last_query();
        return $query->result();
    }

}
